package com.jn.rxjava.presenter;

import android.content.Context;

import androidx.annotation.NonNull;

import com.google.gson.Gson;
import com.jn.rxjava.contract.QAndAContract;
import com.jn.rxjava.entity.QAndAEntity;
import com.jn.rxjava.utils.LogUtils;

import java.io.IOException;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.ObservableEmitter;
import io.reactivex.rxjava3.core.ObservableOnSubscribe;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class QAndAPresenter implements QAndAContract.IQAndAPresenter {
    private Context mContext;
    private QAndAContract.IQAndAView mQAndAView;
    private OkHttpClient mClient;

    public QAndAPresenter(QAndAContract.IQAndAView qAndAView, Context context) {
        mQAndAView = qAndAView;
        mContext = context;
        mClient = new OkHttpClient();
    }

    @Override
    public void getData() {//get方式异步请求
        if (mQAndAView != null) {
            mQAndAView.onHttpLoading(true);
        }
        Observable<QAndAEntity> observable = Observable.create(new ObservableOnSubscribe<QAndAEntity>() {
                    @Override
                    public void subscribe(@io.reactivex.rxjava3.annotations.NonNull ObservableEmitter<QAndAEntity> emitter) throws Throwable {
                        String type = "json";
                        Request request = new Request.Builder()
                                .url("https://shanhe.kim/api/za/shenhuifu.php?type=" + type)
                                //.get()
                                .build();
                        Call call = mClient.newCall(request);
                        call.enqueue(new Callback() {
                            @Override
                            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                                LogUtils.d("asdf", "onFailure");
                            }

                            @Override
                            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                                String body = response.body().string();
                                String url = call.request().url().toString();
                                LogUtils.d("asdf", "url : " + url);
                                LogUtils.d("asdf", "onResponse : " + body);
                                Gson gson = new Gson();
                                QAndAEntity qAndAEntity = gson.fromJson(body, QAndAEntity.class);
                                emitter.onNext(qAndAEntity);
                            }
                        });
                    }
                }).observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io());

        observable.safeSubscribe(new Observer<QAndAEntity>() {
            @Override
            public void onSubscribe(@io.reactivex.rxjava3.annotations.NonNull Disposable d) {

            }

            @Override
            public void onNext(@io.reactivex.rxjava3.annotations.NonNull QAndAEntity qAndAEntity) {
                if (mQAndAView != null) {
                    mQAndAView.onHttpLoading(false);
                    mQAndAView.showData(qAndAEntity);
                }
            }

            @Override
            public void onError(@io.reactivex.rxjava3.annotations.NonNull Throwable e) {
                if (mQAndAView != null) {
                    mQAndAView.onHttpLoading(false);
                    mQAndAView.onHttpFail(e.getMessage());
                }
            }

            @Override
            public void onComplete() {

            }
        });

    }
}
